package com.sentrix.auto.printercenter.infrastructure.persistence.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sentrix.auto.printercenter.domain.entity.PrinterAgentEntity;
import com.sentrix.auto.printercenter.infrastructure.persistence.IAgentRepository;
import com.sentrix.auto.printercenter.infrastructure.persistence.dao.mapper.AgentMapper;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class AgentRepositoryImpl extends ServiceImpl<AgentMapper, PrinterAgentEntity> implements IAgentRepository {
    @Override
    public Optional<PrinterAgentEntity> findByCode(String macAddress) {
        PrinterAgentEntity agentEntity = this.getOne(query().eq(PrinterAgentEntity::getCode, macAddress));
        if (agentEntity != null) {
            return Optional.of(agentEntity);
        }
        return Optional.empty();
    }

    @Override
    public void online(PrinterAgentEntity agentEntity) {
        agentEntity.setIsOnline(Boolean.TRUE);
        this.saveOrUpdate(agentEntity);
    }

    @Override
    public void offline(PrinterAgentEntity agentEntity) {
        agentEntity.setIsOnline(Boolean.FALSE);
        this.updateById(agentEntity);
    }
}
